package com.remrbs.service;

import com.remrbs.Vo.ResultVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.remrbs.mapper.UserMapper;
import com.remrbs.domain.User;
@Service
public class UserService{

    @Resource
    private UserMapper userMapper;
    private ResultVo resultVo = new ResultVo();
    
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    
    public int insert(User record) {
        return userMapper.insert(record);
    }


    public int insertSelective(User record) {

        return userMapper.insertSelective(record);
    }

    public ResultVo selectByPrimaryKey(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        if (user != null){
            resultVo.setFlag("1");
            resultVo.setMessage("查询成功");
            resultVo.setObject(user);
        }else {
            resultVo.setFlag("0");
            resultVo.setMessage("查询失败");
        }
        return resultVo;
    }

    
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    
    public int updateBatch(List<User> list) {
        return userMapper.updateBatch(list);
    }

    public ResultVo checkUsername(String username) {
        User user = userMapper.selectByUsername(username);
        if (user != null){
            resultVo.setFlag("0");
            resultVo.setMessage("用户名已存在!");
        }else {
            resultVo.setFlag("1");
            resultVo.setMessage("用户名可使用!");
        }

        return resultVo;
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    public ResultVo login(String username, String password) {
        //用户名校验,先写着,将来应该会在需要在前端检验,届时将这段注释掉  ,只需要判断与数据库是否一致而已
        if (username == null || username.equals("")){
            resultVo.setFlag("0");
            resultVo.setMessage("用户名为空,请输入有效用户名!");
        }
        User user = userMapper.selectByUsername(username);
        if (user == null){
            resultVo.setFlag("0");
            resultVo.setMessage("用户名不存在");
        }else {
            String dbpwd = user.getPassword();
            if (password == null || password.equals("")){
                resultVo.setFlag("0");
                resultVo.setMessage("密码不能为空!");
            }else if (dbpwd.equals(password)){
                resultVo.setFlag("1");
                resultVo.setMessage("登录成功!");
            }else{
                resultVo.setFlag("0");
                resultVo.setMessage("密码输入有误!");
            }
        }
        return resultVo;
    }

   /* private ResultVo selectByUsernameLike(String username) {
        userMapper.selectByUsernameLike(username);

    }*/
}
